#include"io430.h"
#include"intrinsics.h"
#include"include.h"
#include"pwm.h"
#include"adc.h"
#include"data_flash.h"
#include"data_param.h"
#include"system_init.h" 


extern volatile packet  tx_Buffer;
extern volatile packet  rx_Buffer;

short tmp_flash[20];

extern unsigned char from_adress;  

extern short level_battery;

extern volatile unsigned char flag_run;  
extern volatile unsigned char flag_stop; 

extern volatile unsigned char flag_on_pwm; 
extern volatile unsigned char flag_run_pwm; 
extern volatile unsigned char flag_recieve;

extern unsigned short count_packet;
extern unsigned short tmp_count_packet;

extern int main(void);


void recieve_command(void)
{
  __disable_interrupt(); 
  if (RFReceivePacket((packet*)&rx_Buffer,64))     // Fetch packet from CCxxxx    
   {
    __enable_interrupt();        
      if (((rx_Buffer.header[1])&0x80)!=0x80)
      {
       if (((rx_Buffer.header[1])&0x1F)==0x04)
        {
         if (flag_run_pwm==0)
         {  
           __delay_cycles(250);
           tx_Buffer.length=5;
           tx_Buffer.adress=rx_Buffer.header[0];
           tx_Buffer.header[0]=P_ADRESS;
           tx_Buffer.header[1]=rx_Buffer.header[1]+0x80;
           tx_Buffer.header[2]=rx_Buffer.header[2];
           tx_Buffer.header[3]=rx_Buffer.header[3];      
           RFSendPacket((packet*)&tx_Buffer,tx_Buffer.length+1);
           __delay_cycles(250);
           flag_on_pwm=1;
         }
           return;
        };       
       if (((rx_Buffer.header[1])&0x1F)==0x05)
        {
           __delay_cycles(250);
          tx_Buffer.length=rx_Buffer.length;
          tx_Buffer.adress=rx_Buffer.header[0];
          tx_Buffer.header[0]=P_ADRESS;
          tx_Buffer.header[1]=rx_Buffer.header[1]+0x80;
          tx_Buffer.header[2]=rx_Buffer.header[2];
          tx_Buffer.header[3]=rx_Buffer.header[3]; 
          for (int _i=0;_i<rx_Buffer.length-5;_i++) tx_Buffer.data[_i]=rx_Buffer.data[_i];
          RFSendPacket((packet*)&tx_Buffer,tx_Buffer.length+1);
           for (int _i=0;_i<2;_i++)
           {
            P1OUT&=~BIT2;
            for (char _j=0;_j<100;_j++) __delay_cycles(3000);
            P1OUT|=BIT2;
            for (char _j=0;_j<100;_j++) __delay_cycles(3000);
           };
           P1OUT&=~BIT2;
          return; 
        };

       if (((rx_Buffer.header[1])&0x1F)==0x06)
        {
           __delay_cycles(250);
          tx_Buffer.length=5;
          tx_Buffer.adress=rx_Buffer.header[0];
          tx_Buffer.header[0]=P_ADRESS;
          tx_Buffer.header[1]=rx_Buffer.header[1]+0x80;
          tx_Buffer.header[2]=rx_Buffer.header[2];
          tx_Buffer.header[3]=rx_Buffer.header[3];           
          RFSendPacket((packet*)&tx_Buffer,tx_Buffer.length+1);
          On_ADC_battery();
          return; 
        };

       if (((rx_Buffer.header[1])&0x1F)==0x07)
        {
           __delay_cycles(250);
          tx_Buffer.length=7;
          tx_Buffer.adress=rx_Buffer.header[0];
          tx_Buffer.header[0]=P_ADRESS;
          tx_Buffer.header[1]=rx_Buffer.header[1]+0x80;
          tx_Buffer.header[2]=rx_Buffer.header[2];
          tx_Buffer.header[3]=rx_Buffer.header[3];  
          tx_Buffer.data[0]=level_battery/256;
          tx_Buffer.data[1]=level_battery%256;
          RFSendPacket((packet*)&tx_Buffer,tx_Buffer.length+1);
          return; 
        };

       if (((rx_Buffer.header[1])&0x1F)==0x0d)
        {          
          On_ADC_battery();          
          return; 
        };
       
       if (((rx_Buffer.header[1])&0x1F)==0x09)
        {
           __delay_cycles(250);
          tx_Buffer.length=rx_Buffer.length;
          tx_Buffer.adress=rx_Buffer.header[0];
          tx_Buffer.header[0]=P_ADRESS;
          tx_Buffer.header[1]=rx_Buffer.header[1]+0x80;
          tx_Buffer.header[2]=rx_Buffer.header[2];
          tx_Buffer.header[3]=rx_Buffer.header[3];
          write_flash((unsigned char)rx_Buffer.data[1],(unsigned char*)&rx_Buffer.data[2],(char)rx_Buffer.data[0],(char*)tmp_flash);
          tx_Buffer.data[0]=rx_Buffer.data[0];
          tx_Buffer.data[1]=rx_Buffer.data[1];
          read_flash((unsigned char*)(0x1000+rx_Buffer.data[1]),(unsigned char*)&tx_Buffer.data[2],rx_Buffer.data[0]);
          RFSendPacket((packet*)&tx_Buffer,tx_Buffer.length+1);
          __delay_cycles(250);
          main();
          return;
        };

       if (((rx_Buffer.header[1])&0x1F)==0x0A)
        {
           __delay_cycles(250);
          tx_Buffer.length=15;
          tx_Buffer.adress=rx_Buffer.header[0];
          tx_Buffer.header[0]=P_ADRESS;
          tx_Buffer.header[1]=rx_Buffer.header[1]+0x80;
          tx_Buffer.header[2]=rx_Buffer.header[2];
          tx_Buffer.header[3]=rx_Buffer.header[3];
          read_flash((unsigned char*)(0x1000),(unsigned char*)&tx_Buffer.data,10);         
          RFSendPacket((packet*)&tx_Buffer,tx_Buffer.length+1);
          __delay_cycles(250);
          return;
        };
       
       if (((rx_Buffer.header[1])&0x1F)==0x10)
        {
           __delay_cycles(250);
          tx_Buffer.length=5;
          tx_Buffer.adress=rx_Buffer.header[0];
          tx_Buffer.header[0]=P_ADRESS;
          tx_Buffer.header[1]=rx_Buffer.header[1]+0x80;
          tx_Buffer.header[2]=rx_Buffer.header[2];
          tx_Buffer.header[3]=rx_Buffer.header[3];
          RFSendPacket((packet*)&tx_Buffer,tx_Buffer.length+1);
          __delay_cycles(250);
          return;
        };
       
       if (((rx_Buffer.header[1])&0x1F)==0x11)
        {
           __delay_cycles(250);
          tx_Buffer.length=5;
          tx_Buffer.adress=rx_Buffer.header[0];
          tx_Buffer.header[0]=P_ADRESS;
          tx_Buffer.header[1]=rx_Buffer.header[1]+0x80;
          tx_Buffer.header[2]=rx_Buffer.header[2];
          tx_Buffer.header[3]=rx_Buffer.header[3];
          RFSendPacket((packet*)&tx_Buffer,tx_Buffer.length+1);
          __delay_cycles(250);
          return;
        };
       if (((rx_Buffer.header[1])&0x1F)==0x12)
        {
           __delay_cycles(250);
          tx_Buffer.length=5;
          tx_Buffer.adress=rx_Buffer.header[0];
          tx_Buffer.header[0]=P_ADRESS;
          tx_Buffer.header[1]=rx_Buffer.header[1]+0x80;
          tx_Buffer.header[2]=rx_Buffer.header[2];
          tx_Buffer.header[3]=rx_Buffer.header[3];
          set_power_cc1101(rx_Buffer.data[0]);
          RFSendPacket((packet*)&tx_Buffer,tx_Buffer.length+1);
          __delay_cycles(250);
          return;
        };
      } 
      else
      {
        if (flag_run==1)   TI_CC_SPIStrobe(TI_CCxxx0_SIDLE); else TI_CC_SPIStrobe(TI_CCxxx0_SRX); //TI_CC_SPIStrobe(TI_CCxxx0_SIDLE)
        return;
      };
    }
    else
    {
      TI_CC_SPIStrobe(TI_CCxxx0_SIDLE);
      TI_CC_SPIStrobe(TI_CCxxx0_SFRX);
      TI_CC_SPIStrobe(TI_CCxxx0_SRX);
      __enable_interrupt();
      return;
    }
};